const db = require('../../db')

const apiName = 'allbooks'

const router = require('express').Router()


// 全部图片显示
router.get(`/${apiName}`, (req, res) => {
  // console.log(req.query);
  let pagenum = req.query.pagenum
  let pagesize = req.query.pagesize
  // // 翻页
  let offset = (pagenum - 1) * pagesize

  let sql = `select * from ebook order by id limit ${offset},${pagesize};select count(*) total from ebook`
  db.query(sql, (error, result, fields) => {
    if (error) {
      res.json({
        'code': 400,
        'error': '查询出错'
      })
    } else {
      res.json({
        'code': 200,
        'data': result[0],
        'total': result[1][0].total
      })
      // console.log(result);
    }
  })
})
// 右边侧栏
router.get('/title', (req, res) => {
  let sql = `select title from ebook where title like 'SAP PA教材%' limit 0,15`
  db.query(sql, (error, result, fields) => {
    if (error) {
      res.json({
        'code': 400
      })
    } else {
      res.json({
        'code': 200,
        'data': result
      })
      // console.log(result);
    }
  })
})

// bwbo面图
router.get('/bwbo', (req, res) => {
  let sql = `select * from ebook where fele= 'BWBO';select count(*) total from ebook where fele= 'BWBO'`
  db.query(sql, (error, result, fields) => {
    if (error) {
      res.json({
        'code': 400
      })
    } else {
      res.json({
        'code': 200,
        'data': result[0],
        'total': result[1][0].total
      })
    }
  })
})



module.exports = router